﻿Imports Kairyu.Load
Imports Kairyu.Save

Namespace Connection

    ''' <summary>
    ''' DB接続処理デリゲート
    ''' </summary>
    ''' <remarks>
    ''' 処理を短く書きたい人用。
    ''' </remarks>
    Public Class DB

        Public Shared Sub Connect(core As Action(Of ModelLoader))
            Using cn = Connector.Instance.Connect
                core.Invoke(cn.Loader)
                cn.Close()
            End Using
        End Sub

        Public Shared Function Connect(Of T)(core As Func(Of ModelLoader, T)) As T
            Using cn = Connector.Instance.Connect
                Return core.Invoke(cn.Loader)
            End Using
        End Function

        Public Shared Sub Transact(core As Action(Of ModelSaver))
            Using cn = Connector.Instance.Connect
                Using trn = cn.BeginTransaction
                    Try
                        core.Invoke(trn.Saver)
                        trn.Commit()
                    Catch ex As Exception
                        trn.Rollback()
                        Throw
                    Finally
                        cn.Close()
                    End Try
                End Using
            End Using
        End Sub

    End Class

End Namespace
